﻿using ServiceMs.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;

namespace ServiceMs.Base
{
    public static class ServerMethod
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="serverInfo"></param>
        public static void Execute(string methodName, ServerInfo serverInfo)
        {
            LogHelper.WriteLog("--------------------------------------任务开始执行-------------------------------------", serverInfo.TaskName);
            var type = typeof(ServerMethod);
            var method = type.GetMethod(methodName);
            if (method != null)
            {
                method.Invoke(null, new object[] { serverInfo });
            }
            else
            {
                LogHelper.WriteLog("未查找到该任务执行方法", serverInfo.TaskName);
            }
            LogHelper.WriteLog("--------------------------------------任务执行结束-------------------------------------", serverInfo.TaskName);
        }

        /// <summary>
        /// 执行api调度
        /// </summary>
        /// <param name="methodServerInfo"></param>
        public static void ExcuteApi(ServerInfo methodServerInfo)
        {
            LogHelper.WriteLog("--------------------------------------任务开始执行-------------------------------------", methodServerInfo.TaskName);
            var result = HttpOpration.HttpRequestGet(methodServerInfo.TaskMethod);
            LogHelper.WriteLog("接口调用结果:" + result, methodServerInfo.TaskName);
            LogHelper.WriteLog("--------------------------------------任务执行结束-------------------------------------", methodServerInfo.TaskName);
        }

        /// <summary>
        /// 定时测试方法
        /// </summary>
        public static void TestMethod(object serverInfo)
        {
            LogHelper.WriteLog("测试任务执行完成", ((ServerInfo)serverInfo).TaskName);
        }

        public static void ClearTaskLog(ServerInfo serverInfo)
        {
            try
            {
                var logPath = ConfigurationManager.AppSettings["LogPath"];
                var retainDays = ConfigurationManager.AppSettings["RetainLogDays"];
                if (string.IsNullOrEmpty(logPath))
                {
                    throw new Exception("未配置日志写入路径！");
                }
                if (string.IsNullOrEmpty(retainDays))
                {
                    throw new Exception("未配置日志保留天数！");
                }
                int retainDaysInt = Convert.ToInt32(retainDays);

                DateTime startDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).AddDays(-retainDaysInt);

                var directoryArray = Directory.GetDirectories(logPath);
                foreach (string path in directoryArray)
                {
                    if (path == (logPath + "\\SysLogs")) continue;
                    var fileArray = Directory.GetFiles(path);
                    if (fileArray.Length == 0) continue;

                    foreach (string filePath in fileArray)
                    {
                        var fileName = Path.GetFileNameWithoutExtension(filePath);
                        var createFileDate = Convert.ToDateTime(fileName);
                        if (DateTime.Compare(startDate, createFileDate) >= 0)
                        {
                            File.Delete(filePath);
                            LogHelper.WriteLog("日志文件清理成功,清理文件：" + filePath, serverInfo.TaskName);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("日志清理遇到错误：" + ex.Message, serverInfo.TaskName);
            }
        }

    }
}